//
//  IDELinkViewController.m
//  HGMMT
//
//  Created by eport_user1 on 2018/4/13.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "IDELinkViewController.h"
#import "LinkServers.h"
#import "AppDelegate.h"




@interface IDELinkViewController ()
@property (weak, nonatomic) IBOutlet UIButton *linkBtn;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

@property (weak, nonatomic) IBOutlet UITextField *linkTextField;
@end

@implementation IDELinkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self _initView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

//连接按钮事件
- (IBAction)linkButtonAction:(UIButton *)sender {
    
    if (self.linkTextField.text.length == 0) {
        
        
        UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的ide不能为空" preferredStyle:(UIAlertControllerStyleAlert)];
        [altervc addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]];
        
        [self presentViewController:altervc animated:YES completion:nil];
        
        
        return;
    }
    
    
   // NSString *ipFormats = nil;
    
    if ([self.linkTextField.text containsString:@"-"])
    {
       // NSString *ipFormats = [self.linkTextField.text formatIpPorts:self.linkTextField.text];
    
        [[LinkServers shareInstance] linkWithIdeCode:self.linkTextField.text];
        
        [LinkServers shareInstance].linkBlock = ^(BOOL isSuccess) {
            
            if (isSuccess == NO) {
                
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"连接失败" message:@"请检查测试门户与开发套件是否处于同一网络下" preferredStyle:(UIAlertControllerStyleAlert)];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }]];
                
                [self presentViewController:alert animated:YES completion:nil];
            }
            
        };
        
    }
    else
    {
        UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的输入不合规" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            self.linkTextField.text = @"";
            
        }]];
        
        [self presentViewController:alerVC animated:YES completion:^{
            
        }];
        
    }
 
}



//取消按钮事件
- (IBAction)cancelButtonAction:(UIButton *)sender {
    
    self.linkTextField.text = @"";

}

-(void)_initView{
    
    self.title = @"输入IDE识别码";
    self.linkBtn.backgroundColor = HGNavBarColor;
    self.cancelBtn.backgroundColor = HGNavBarColor;
    self.linkBtn.layer.borderColor = [UIColor blackColor].CGColor;
    self.linkBtn.layer.borderWidth = 1;
    self.linkBtn.layer.cornerRadius = 5;
    self.linkBtn.layer.masksToBounds = YES;
    self.cancelBtn.layer.borderColor = [UIColor blackColor].CGColor;
    self.cancelBtn.layer.borderWidth = 1;
    self.cancelBtn.layer.cornerRadius = 5;
    self.cancelBtn.layer.masksToBounds = YES;
    self.linkTextField.layer.borderColor = [UIColor blackColor].CGColor;
    self.linkTextField.layer.borderWidth = 1;
    self.linkTextField.layer.cornerRadius = 5;
    self.linkTextField.layer.masksToBounds = YES;
    
}








@end
